home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_09 / barbu2 / descript.cpp < prev    next >
C/C++ Source or Header  |  1996-06-18  |  3KB  |  150 lines

  1. /////////////////////////////////////////////////////
  2. //    DESCRIPT implementation
  3. /////////////////////////////////////////////////////
  4. #include "DESCRIPT.HPP"
  5. #include <stdlib.h>
  6.  
  7. DESCRIPT::DESCRIPT(const char szS[]) : STR (szS){}
  8. DESCRIPT::~DESCRIPT() {}
  9.  
  10. short DESCRIPT::isSep(int c, int k) const
  11. {
  12. if(operator[](k) != c)
  13.     return 0;
  14. if(k == 0)
  15.     return 1;
  16. return operator[](k-1) != LIT;
  17. }
  18.  
  19. const char * DESCRIPT::retVal(STR& _buf) const
  20. {
  21. int i, j;
  22. char *p = (char*)(const char*)_buf;
  23.  
  24. for(i = 0; p[i]; i++){
  25.     if(p[i] == LIT){
  26.         for(j = i; p[j]; j++)
  27.             p[j] = p[j+1];
  28.         }
  29.     }
  30. return p;
  31. }
  32.  
  33. const char * DESCRIPT::type(STR& _buf) const
  34. {
  35. _buf = "";
  36. short i = hasin(SLASH);
  37. if(i == -1){
  38.     _buf = *this;
  39.     return retVal(_buf);
  40.     }
  41. for(short j = 0; j < i; j++)
  42.     _buf += operator[](j);
  43. return retVal(_buf);
  44. }
  45.  
  46. const char * DESCRIPT::value(const char szParam[],
  47.                             STR& _buf) const
  48. {
  49. short last, i, j;
  50. _buf = "";
  51. i = 0;
  52. for(last = 0 ; -1 != (i = hasin(SLASH, last+1)) ;
  53.                                     last = i){
  54.     if(!isSep(SLASH, i))
  55.         continue;
  56.     if(i+1 == hasin(szParam, i)){
  57.         for(short k = i+1 ;
  58.             operator[](k) && !isSep(SLASH, k)
  59.                             && !isSep(EQUAL,k) ;
  60.             k++)
  61.             ;
  62.         if(isSep(EQUAL, k)){
  63.             for(j = k+1 ; operator[](j)
  64.                             && !isSep(SLASH, j); j++)
  65.                 _buf += operator[](j);
  66.             return retVal(_buf);
  67.             }
  68.         }
  69.     }
  70. return 0;
  71. }
  72.  
  73. int DESCRIPT::value(const char szParam[], int nDef)
  74.                     const
  75. {
  76. STR _buf;
  77. if(0 == value(szParam, _buf))
  78.     return nDef;
  79. return atoi(retVal(_buf));
  80. }
  81.  
  82.  
  83. void DESCRIPT::setType(const char szType[])
  84. {
  85. *(STR*)this = szType;
  86. }
  87.  
  88. void DESCRIPT::addParam(const char szParam[],
  89.                     const char szValue[])
  90. {
  91. *this += SLASH;
  92. *this += szParam;
  93. *this += EQUAL;
  94. for(int i = 0; szValue[i]; i++){
  95.     if(szValue[i] == SLASH || szValue[i] == EQUAL ||
  96.                         szValue[i] ==  LIT)
  97.         *this += LIT;
  98.     *this += szValue[i];
  99.     }
  100. }
  101.  
  102. void DESCRIPT::addParam(const char szParam[],
  103.                         int nValue)
  104. {
  105. STR _buf(20);
  106. itoa(nValue, (char*)(const char*)_buf, 10);
  107. *this += SLASH;
  108. *this += szParam;
  109. *this += EQUAL;
  110. *this += _buf;
  111. }
  112.  
  113.  
  114. MULTIPARAM::MULTIPARAM(const DESCRIPT* Desc,
  115.                     const char szMP[])
  116.     : _desc(Desc), _mp(szMP)
  117. {
  118. STR Buf;
  119. int i;
  120. for(i = 0; 0 != _desc->value(_makeParam(i), Buf);
  121.     i++)
  122.     ;
  123. _n = i;
  124. }
  125.  
  126. const char* MULTIPARAM::value(int x, STR& Buf)
  127. {
  128. if(x < 0 || _n <= x)
  129.     return 0;
  130. return _desc->value(_makeParam(x), Buf);
  131. }
  132.  
  133. int MULTIPARAM::value(int x, int nDefVal)
  134. {
  135. if(x < 0 || _n <= x)
  136.     return 0;
  137. return _desc->value(_makeParam(x), nDefVal);
  138. }
  139.  
  140. const char* MULTIPARAM::_makeParam(int index)
  141. {
  142. char sz[16];
  143. itoa(index, sz, 10);
  144. _full = _mp;
  145. _full += sz;
  146. return _full;
  147. }
  148.  
  149.  
  150.